package com.kernel.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/****************************************
 * @name ConnectionDemo01
 * @description
 * @author 黄彬恒
 * @since 2018/8/23
 ***************************************/
public class ConnectionDemo01 {
    /*定义MySQL数据库的驱动程序*/
    public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    /*定义MySQL数据库的连接地址*/
    public static final String DBURL = "jdbc:mysql://127.0.0.1:3306/dbgirl";
    /*MySQL连接用户名*/
    public static final String DBUSER = "root";
    /*MySQL连接密码*/
    public static final String DBPASSWORD = "123456";

    public static void main(String[] args) {
        // try {
        //     Class<?> clazz = Class.forName(DBDRIVER);// 手动加载驱动程序(检验驱动程序classpath是否成功配置)
        //     System.out.println(clazz.getName());
        // } catch (ClassNotFoundException e) {
        //     e.printStackTrace();
        // }
        Connection conn = null; // 声明数据库连接
        PreparedStatement pstmt = null;// 数据库预处理操作(预防SQL注入攻击)
        String sql = "INSERT INTO `girl` (`age`, `cup_size`, `money`) VALUES (?, ?,?)";
        try {
            conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);// 建立数据库连接
            pstmt = conn.prepareStatement(sql);// 实例化PreparedStatement对象
            pstmt.setInt(1, 20);
            pstmt.setString(2, "B");
            pstmt.setDouble(3, 0.01);
            pstmt.executeUpdate();// 执行数据库新增、修改、删除等操作
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
